<?php
    //$con = mysql_connect("localhost","root","pass");
	//echo $con;
	
	$_user = "root";
	$_time = date("Y/m/d h:i:s");
	$_message = "It's a message";
	
	$newMessageInfo = new MessageInfo($_user,$_time,$_message);
	
	
	error_reporting(E_NOTICE);
	
	//SaveMessage($newMessageInfo);
	//LoadMessage();
	DeleteMessage($newMessageInfo);
	
	//Load the last ten message from xml
	function LoadMessage()
	{
		$dom = new DOMDocument("1.0","UTF-8");
		$fileName = "../XML/MessageBoard.xml";
		$tag = $dom->load($fileName);
		//echo $tag;
		$MessageInfos = $dom->getElementsByTagName("MessageInfo");
		
		for ($i = $MessageInfos->length; $i > 0 && $i > $MessageInfos->length - 10; $i--) {
			$username = $MessageInfos->item($i-1)->getElementsByTagName("UserName")->item(0)->nodeValue;
			$dateTime = $MessageInfos->item($i-1)->getElementsByTagName("DateTime")->item(0)->nodeValue;
			$message = $MessageInfos->item($i-1)->getElementsByTagName("Message")->item(0)->nodeValue;
			echo $i;
			echo "<p><h1>" . $username . "</h1></p>" .
			"<p><h2>" . $dateTime . "</h2></p>" .
			"<p><h3>" . $message . "</h3></p>";	
		}
	}
	
	//Save message to xml
	function SaveMessage($messageInfo)
	{
		$dom = new DOMDocument("1.0","UTF-8");
		$dom->formatOutput = TRUE;
		$dom->preserveWhiteSpace = FALSE;
		$filepath = "../XML/MessageBoard.xml";
		$tag = $dom->load($filepath);
		
		if($tag==FALSE)
		{
			$eMessageInfos = $dom->createElement("MessageInfos");
			$dom->appendChild($eMessageInfos);
		}
		
		//Create the Element for the new MessageInfo
		$eMessageInfo = $dom->createElement("MessageInfo");
		$eUserName = $dom->createElement("UserName",$messageInfo->m_userName);
		$eDateTime = $dom->createElement("DateTime",$messageInfo->m_dateTime);
		$eMessage = $dom->createElement("Message",$messageInfo->m_message);
		
		$eMessageInfo->appendChild($eUserName);
		$eMessageInfo->appendChild($eDateTime);
		$eMessageInfo->appendChild($eMessage);
		$dom->firstChild->appendChild($eMessageInfo);
		
		$dom->save($filepath);
		echo "Successed";
	}
	
	//Delete message in the xml, the condition is UserName and DateTime
	function DeleteMessage($messageInfo)
	{
		$dom = new DOMDocument("1.0","UTF-8");
		$dom->formatOutput = TRUE;
		$dom->preserveWhiteSpace = FALSE;
		$filepath = "../XML/MessageBoard.xml";
		$tag = $dom->load($filepath);
		
		if($tag == TRUE)
		{
			$eMessageInfos = $dom->getElementsByTagName("MessageInfo");
			foreach ($eMessageInfos as $eMessageInfo) {
				$userName = $eMessageInfo->getElementsByTagName("UserName")->item(0)->nodeValue;
				$dateTime = $eMessageInfo->getElementsByTagName("DateTime")->item(0)->nodeValue;
				
				if($messageInfo->m_userName == $userName && $messageInfo->m_dateTime == $dateTime)
				{
					//echo $messageInfo->m_message;
					
					$dom->firstChild->removeChild($eMessageInfo);
					echo "Remove";
					break;
				}
				else 
				{
					echo "<b>Not Existed</b>";
				}
			}
			$dom->save($filepath);
		}
		
	}
	
	
	
	/**
	 * MessageInfo Contain UserName,DateTime and Message
	 */
	class MessageInfo {
		//Members
		var $m_userName;
		var $m_dateTime;
		var $m_message;	
		//Construct
		function __construct($username,$datetime,$message) {
			$this->m_userName = $username;
			$this->m_dateTime = $datetime;
			$this->m_message = $message;
		}
	}
?>